
include( 'shared.lua' )
include( 'resources.lua' )
include( 'cl_HUD.lua' )



hook.Add("SpawnMenuOpen", "adminonly", function()
      if LocalPlayer():IsAdmin() then
            return true
      end
end);

hook.Add( "SpawnMenuOpen", "DisallowSpawnMenu", DisallowSpawnMenu)

function GM:PlayerSpray(ply)
	return !ply:IsAdmin()
	ply:AllowImmediateDecalPainting()
end

function motd( um )

	local DermaPanel = vgui.Create( "DFrame" )
	DermaPanel:SetPos( 150,50 )
	DermaPanel:SetSize( 600, 500 )
	DermaPanel:SetTitle( "Message of the day" ) -- Title of the frame
	DermaPanel:SetVisible( true )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	DermaPanel:MakePopup( true )

	function DermaPanel:Paint()
		surface.SetDrawColor( 0, 0, 0, 225 ) -- Set our rect color below us; we do this so you can see items added to this panel
		surface.DrawRect( 0, 0, DermaPanel:GetWide(), DermaPanel:GetTall() ) -- Draw the rect
	end


	local PropertySheet = vgui.Create( "DPropertySheet", DermaPanel )
	PropertySheet:SetPos( 10, 30 )
	PropertySheet:SetSize( 580, 460 )

	local p2 = vgui.Create( "HTML", DermaPanel )
	p2:SetPos( 0, 0 )
	p2:SetSize( 580, 460 )
	p2:SetHTML( file.Read( "CG_FUN/admins.txt" ))

	local motd = vgui.Create( "HTML", DermaPanel )
	motd:SetPos( 0, 0 )
	motd:SetSize( 580, 460 )
	motd:SetHTML( file.Read( "CG_FUN/motd.txt" ) )


	PropertySheet:AddSheet( "Message of the day", motd, "gui/silkicons/world", false, false, "Message of the day" )
	PropertySheet:AddSheet( "Admins", p2, "gui/silkicons/group", false, false, "The admins you need to complain to" )

end

usermessage.Hook("open_motd", motd)


function mahpanel( um )

	local DermaPanel = vgui.Create( "DFrame" )
	DermaPanel:SetPos( 50, 50 )
	DermaPanel:SetSize( 600, 500 )
	DermaPanel:SetTitle( "                                                             -----Citron Gamers Multi Information Desk-----" ) -- Title of the frame
	DermaPanel:SetVisible( true )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	DermaPanel:MakePopup( true )

	function DermaPanel:Paint()
		surface.SetDrawColor( 0, 0, 0, 225 ) -- Set our rect color below us; we do this so you can see items added to this panel
		surface.DrawRect( 0, 0, DermaPanel:GetWide(), DermaPanel:GetTall() ) -- Draw the rect
	end


	local PropertySheet = vgui.Create( "DPropertySheet", DermaPanel )
	PropertySheet:SetPos( 10, 30 )
	PropertySheet:SetSize( 580, 460 )

	local p1 = vgui.Create( "DPanel", DermaPanel )
	p1:SetPos( 0, 0 )
	p1:SetSize( 580, 460 )

	local p2 = vgui.Create( "HTML", DermaPanel )
	p2:SetPos( 0, 0 )
	p2:SetSize( 580, 460 )
	p2:SetHTML( file.Read( "CG_FUN/admins.txt" ))

	local p3 = vgui.Create( "DPanel", DermaPanel )
	p3:SetPos( 0, 0 )
	p3:SetSize( 580, 460 )

	local motd = vgui.Create( "HTML", DermaPanel )
	motd:SetPos( 0, 0 )
	motd:SetSize( 580, 460 )
	motd:SetHTML( file.Read( "CG_FUN/motd.txt" ) )

	local myLabel= vgui.Create("DLabel", p1)
	myLabel:SetText("This is the Store of Citron Gamers. It is not in use for the moment though")
	myLabel:SizeToContents() // make the control the same size as the text.
	myLabel:SetPos(2,30)
	myLabel:SetTextColor( Color(0, 0, 0, 255) )

	local myLabel2= vgui.Create("DLabel", p1)
	myLabel2:SetText("I have just added the option to trail yourself that works with an earlier mod i made :D")
	myLabel2:SizeToContents() // make the control the same size as the text.
	myLabel2:SetPos(2,45)
	myLabel2:SetTextColor( Color(0, 0, 0, 255) )

	local button = vgui.Create( "DButton", p1 );
	button:SetSize( 100, 30 );
	button:SetPos( 50, 70 );
	button:SetText( "Trail Me" );
	button.DoClick = function( button )
		RunConsoleCommand("~trailme")
	end
	button:SetTextColor( Color(255, 255, 255, 255) )

	local myLabel3= vgui.Create("DLabel", p1 )
	myLabel3:SetText("Sadly, You cannot 'remove' the trail at this time, so you will have to find enougher way to hide from ")
	myLabel3:SizeToContents() // make the control the same size as the text.
	myLabel3:SetPos(2,107)
	myLabel3:SetTextColor( Color(0, 0, 0, 255) )

	local myLabel4= vgui.Create("DLabel", p1 )
	myLabel4:SetText("admins apart from being invisible ;P")
	myLabel4:SizeToContents() // make the control the same size as the text.
	myLabel4:SetPos(2,122)
	myLabel4:SetTextColor( Color(0, 0, 0, 255) )


	local button2 = vgui.Create( "DButton", p1 );
	button2:SetSize( 150, 30 );
	button2:SetPos( 50, 143 );
	button2:SetText( "Connect to Server 1" );
	button2.DoClick = function( ply, button )
		LocalPlayer():ConCommand("connect server.citrongamers.com:27029");
	end
	button2:SetTextColor( Color(255, 255, 255, 255) )

	local myLabel5= vgui.Create("DLabel", p1 )
	myLabel5:SetText("Now you can show your FPS, this is a VERY useful command!")
	myLabel5:SizeToContents() // make the control the same size as the text.
	myLabel5:SetPos(2,180)
	myLabel5:SetTextColor( Color(0, 0, 0, 255) )

	local myCheckBox = vgui.Create( "DCheckBoxLabel", p1 )
	myCheckBox:SetPos( 10, 210 )
	myCheckBox:SetText( "Show FPS ?" )
	myCheckBox:SetConVar( "cl_showfps" )
	myCheckBox:SetValue( 1 )
	myCheckBox:SizeToContents()
	myCheckBox:SetTextColor( Color(0, 0, 0, 255) )

	shop1 = vgui.Create("DImageButton", p3)
	shop1:SetMaterial( "VGUI/entities/npc_mossman" )
	shop1:SetSize(100, 100)
	shop1.DoClick = function()
		Msg("You clicked the button!\n")
	end
	shop1.DoRightClick = function()
        Msg("You right-clicked the button!\n")
    end

	shop2 = vgui.Create("DImageButton", p3)
	shop2:SetMaterial( "VGUI/shopentities/trail-1" )
	shop2:SetPos(103, 0)
	shop2:SetSize(100, 100)
	shop2.DoClick = function()
		RunConsoleCommand("trailme")
	end
	shop2.DoRightClick = function()
        RunConsoleCommand("trailme")
    end


	PropertySheet:AddSheet( "General Information", p1, "gui/silkicons/table_edit", false, false, "Everything and Anything" )
	PropertySheet:AddSheet( "Admins", p2, "gui/silkicons/group", false, false, "The admins you need to complain to" )
	PropertySheet:AddSheet( "Website", motd, "gui/silkicons/world", false, false, "This is our website" )
	PropertySheet:AddSheet( "Shop", p3, "gui/silkicons/emoticon_smile", false, false, "Buy Stuff Here" )


end

usermessage.Hook("open_shoppanel", mahpanel)



function GM:HUDPaint()
	draw.RoundedBox(2, 0, 0, ScrW(), 25, Color(0, 0, 0, 225))
	draw.SimpleText("You are playing the Lobby gamemode made by Ningaglio, Meisno and Alien. Please have fun and spread the word about this server!", "ScoreboardText", ScrW() / 2, 5, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
	draw.RoundedBox(2, 0, ScrH()-25, ScrW(), 25, Color(0, 0, 0, 225))
	draw.SimpleText("Use F3 or the information desk to open the help menu, the shop, a list of the admins and the MOTD", "ScoreboardText", ScrW() / 2, ScrH()-20, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
end


function hidehud(name)
	for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do
		if name == v then return false end
	end
end
hook.Add("HUDShouldDraw", "hidehud", hidehud)


function helppanel()
	local help = vgui.Create( "DFrame" )
	help:SetPos( 50, 50 )
	help:SetSize( 600, 500 )
	help:SetTitle( "The help desk" ) -- Title of the frame
	help:SetVisible( true )
	help:SetDraggable( true )
	help:ShowCloseButton( true )
	help:MakePopup( true )

	function help:Paint()
		surface.SetDrawColor( 0, 0, 0, 225 ) -- Set our rect color below us; we do this so you can see items added to this panel
		surface.DrawRect( 0, 0, help:GetWide(), help:GetTall() ) -- Draw the rect
	end

	local helper = vgui.Create( "DPanel", help )
	helper:SetPos( 25, 25 )
	helper:SetSize( 550, 450 )
	helper:SetHTML( file.Read( "CG_FUN/help.txt" ) )

end

usermessage.Hook("help", helppanel)

surface.CreateFont( "Tahoma", 16, 1000, true, false, "DarklandChatBox" )
function FluctuateColor(a)
	a = a or 1
	return math.abs(math.sin(CurTime()*a))
end
teamchat = false
chaton = false

local realtext = ""
local chattext = ""
local maxlines = 10
local history = {}
local cursorpos = 0

local COLOR_CHAT = Color( 255, 255, 255, 255 )
local COLOR_CONSOLE = Color( 217, 0, 25, 255 )
local COLOR_WHISPER = Color( 101, 202, 36, 255 )
local COLOR_NOTIFY = Color( 101, 202, 36, 255 )
local COLOR_HINT = Color( 77, 131, 240, 255 )




function AddHistory( pl, msg, type )

	type = tonumber(type) or 0

	local col = COLOR_CONSOLE
	if type == 1 then
		col = COLOR_CHAT
	elseif type == 2 then
		col = COLOR_WHISPER
	elseif type == 3 then
		col = COLOR_NOTIFY
	elseif type == 4 then
		col = COLOR_HINT
	end
		if pl == 0 then pl = nil end
		if tonumber(pl) == pl  then
			for k, v in pairs( player.GetAll() ) do
				if v:UserID() == pl then
					pl = v
					break
				end
			end
		end
	if pl and pl:IsPlayer() and pl.UserID then
			local r,g,b,a = pl:GetColor()
			local color = Color( r, g, b, 255 )
			if (	r == 255	and
				g == 255	and
				b == 255	and
				a == 255	) then
				color = team.GetColor( pl:Team() )
			end
			local chatInfo = {
				pl = pl,
				admin = pl:IsAdmin(),
				name = pl:GetName(),
				color = color,
				textcolor = col,
				msg = msg,
				type = type or 1,
				time = RealTime()+10
			};
		if type == 5 then
			print("PM from " .. pl:GetName().." - "..msg)
		elseif type == 6 then
			chatInfo = {
				pl = pl,
				admin = false,
				name = "(Advert)",
				color = Color(46,116,24,255),
				textcolor = Color(255,255,255,255),
				msg = msg,
				type = type or 1,
				time = RealTime()+10
			};
		elseif type == 7 then
			chatInfo = {
				pl = pl,
				admin = false,
				name = "(Request)",
				color = Color(66,218,241,255),
				textcolor = Color(255,255,255,255),
				msg = msg,
				type = type or 1,
				time = RealTime()+10
			};
		elseif type == 8 then
			chatInfo = {
				pl = pl,
				admin = false,
				name = "(Radio)",
				color = Color(43,117,208,255),
				textcolor = Color(255,255,255,255),
				msg = msg,
				type = type or 1,
				time = RealTime()+10
			};
		elseif type == 9 then
			chatInfo = {
				pl = pl,
				admin = false,
				name = "(Broadcast)",
				color = Color(225,96,51,255),
				textcolor = Color(255,255,255,255),
				msg = msg,
				type = type or 1,
				time = RealTime()+10
			};
		elseif type == 10 then
			chatInfo = {
				admin = false,
				color = Color(225,128,0,255),
				textcolor = Color(255,128,0,255),
				msg = pl:GetName().." "..msg,
				type = type or 1,
				time = RealTime()+10
			};
			print(pl:GetName().." "..msg)
		elseif type == 11 then
			chatInfo = {
				pl = pl,
				admin = false,
				name = "(Gang)",
				color = Color(80,80,80,255),
				textcolor = Color(255,255,255,255),
				msg = msg,
				type = type or 1,
				time = RealTime()+10
			};
		elseif type == 12 then
			chatInfo = {
				pl = pl,
				admin = true,
				name = pl:Name(),
				color = Color(43,117,208,255),
				textcolor = Color(120,120,255,255),
				msg = msg,
				type = type or 1,
				time = RealTime()+10
			};
		end
		table.insert(history,1,chatInfo)
	else
			local chatInfo = {
			textcolor = col,
			msg = msg,
			type = type or 1,
			time = RealTime()+10
		};
		table.insert(history,1,chatInfo)
	end
	history[maxlines+1] = nil

end

usermessage.Hook("__chatprint",function(um) local msg = um:ReadString() AddHistory(NULL,msg,0) print(msg) end)
function OnPlayerChat( player, msg )
	if !IsValid(player) then
		AddHistory( nil, msg, 0 )
		print(msg)
	else
		local name = player:Name()
		AddHistory( player, msg, 1 )
		MsgN(name..": "..msg)
	end
	return true
end
hook.Add("OnPlayerChat","SeriousChat",OnPlayerChat)
function TW(s,f)
	local w = surface.GetTextSize(s,f)
	return w
end
function ChatPaint()

	local x, y = 80, ScrH()-140
	if ChatOffset then
		x = ChatOffset.x or x
		y = ChatOffset.y or y
	end
	local num = 0
	local total = 0
	for i,v in pairs(history) do
		if v.time > RealTime() or chaton then
			if v.pl then
				local x2 = x
				if v.admin and v.pl:GetNWBool("disguised") == false then
					draw.SimpleText("(Admin)","DarklandChatBox",x-4,(y-i*15)+1,Color(0,0,0,255),2)
					draw.SimpleText("(Admin)","DarklandChatBox",x-5,(y-i*15),Color(FluctuateColor(2)*255,0,0,255),2)
				end
				draw.SimpleText(v.name,"DarklandChatBox",x+1,(y-i*15)+1,Color(0,0,0,255))
				draw.SimpleText(v.name,"DarklandChatBox",x,y-i*15,v.color)

				x2 = x2+TW(v.name,"DarklandChatBox")+1
				draw.SimpleText(": "..v.msg,"DarklandChatBox",x2+1,(y-i*15)+1,Color(0,0,0,255))
				draw.SimpleText(": "..v.msg,"DarklandChatBox",x2,y-i*15,v.textcolor)
			else
				draw.SimpleText(v.msg,"DarklandChatBox",x+1,(y-i*15)+1,Color(0,0,0,255))
				draw.SimpleText(v.msg,"DarklandChatBox",x,y-i*15,v.textcolor)
			end
		end
	end

	-- Draw text input
	if chaton then

		local prefix = "Say"
		if string.find( realtext, "//" ) == 1 then
			prefix = "OOC"
		elseif string.find( realtext, "/y" ) == 1 then
			prefix = "Yell"
		elseif string.find( realtext, "/w" ) == 1 then
			prefix = "Whisper"
		elseif teamchat then
			prefix = "Team"
		elseif string.find( realtext, "/" ) == 1 then
			prefix = "Command"
		end
		local endstring = prefix..": "..chattext

		local w, h = surface.GetTextSize( endstring )

		if prefix ~= "Cmd" then
			cursorpos = w
		end

		draw.RoundedBox( 4, x-5, y+5, w+14, h+1, Color( 0, 0, 0, 200 ) )

		surface.SetTextColor( 255, 255, 255, 255 )

		surface.SetTextPos( x+2, y+5 )
		surface.DrawText( endstring )

		if ((math.sin(CurTime() * 5) * 10) > 0) then
			surface.SetTextPos( x+cursorpos+1, y+5 )
			surface.DrawText( "|" )
		end

	end

end
hook.Add("HUDPaint","DrawChat",ChatPaint)
function StartChat()
	chaton = true
	return true
end
hook.Add("StartChat","SeriousStartChat",StartChat)
function FinishChat()
	chaton = false
	teamchat = false
	chattext = ""
	realtext = ""
end
hook.Add("FinishChat","SeriousFinishChat",FinishChat)
function ChatTextChanged( text )
	realtext = text
	if string.find( text, "/" ) ~= 1 then
		chattext = text
	else
		if string.len(text) > 1 then
			chattext = string.Right( text, string.len(text)-1 )
			chattext = CommandHelp( chattext )
		else
			chattext = ""
			chattext = CommandHelp( chattext )
		end
	end
end
hook.Add("ChatTextChanged","ChatMoved",ChatTextChanged)
function CommandHelp( command )
	surface.SetFont( "ScoreboardText" )
	local prefix = "Cmd: "
	local text = command
	local sep = string.Explode(" ", text)
	local w, h = surface.GetTextSize( prefix..text )
	cursorpos = w
	if sep[1] == "givemoney" then
		if !sep[2] then
			sep[2] = "{Amount}"
		else
			sep[2] = "{"..sep[2].." }"
			w, h = surface.GetTextSize( prefix..text.."{" )
			cursorpos = w
		end
	elseif string.find(sep[1],"/") == 1 then
		sep[1] = string.sub(sep[1],2)
	elseif sep[1] == "w" then
		sep[1] = ""
	elseif sep[1] == "y" then
		sep[1] = ""
	end

	return string.Implode( " ", sep )
end

function StartTeamChat(ply, bind)
	if string.find( bind, "messagemode2" ) then
		teamchat = true
	end
end
hook.Add("PlayerBindPress", "TeamChat", StartTeamChat )
